package com.lsl.test1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lishuailong
 * @create 2020-07-06 10:47
 */
public class LockTest1 {
    public static void main(String[] args) {
        Window1 windowx = new Window1();

        Thread thread1 = new Thread(windowx);
        thread1.setName("窗口1");
        Thread thread2 = new Thread(windowx);
        thread2.setName("窗口2");
        Thread thread3 = new Thread(windowx);
        thread3.setName("窗口3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}


class Window1 implements Runnable {
    private int p = 100;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){
            try {
                lock.lock();
                if (p>0){
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName()+"卖出了"+p+"号票！");
                    p--;

                }else {
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}